//
//  ProjectTotalViewController.m
//  Againstpoor
//
//  Created by 辰领科技 on 16/11/8.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "ProjectTotalViewController.h"
#import "ProjectTotalTableViewCell.h"
#import "ProjectManagementSonViewController.h"

static NSString * tableviewIndentifer = @"tableviewCell";

@interface ProjectTotalViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIScrollView *scroview;

@property (weak, nonatomic) IBOutlet UIView *navViewOne;
@property (weak, nonatomic) IBOutlet UILabel *oneLabel;
@property (weak, nonatomic) IBOutlet UILabel *twoLabel;
@property (weak, nonatomic) IBOutlet UILabel *sanLabel;
@property (weak, nonatomic) IBOutlet UILabel *threeLabel;
@property (weak, nonatomic) IBOutlet UILabel *fourLabel;
@property (weak, nonatomic) IBOutlet UILabel *fiveLabel;


@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *buttonOne;
@property (weak, nonatomic) IBOutlet UIButton *buttonTwo;
@property (weak, nonatomic) IBOutlet UIButton *buttonThree;
@property (weak, nonatomic) IBOutlet UILabel *labelOne;
@property (weak, nonatomic) IBOutlet UILabel *labelTwo;
@property (weak, nonatomic) IBOutlet UILabel *labelThree;
@property (weak, nonatomic) IBOutlet UILabel *labelFour;
@property (weak, nonatomic) IBOutlet UILabel *labelFive;
@property (weak, nonatomic) IBOutlet UIButton *imageButtonOne;
@property (weak, nonatomic) IBOutlet UIButton *imageButtonTwo;
@property (weak, nonatomic) IBOutlet UIButton *imageButtonThree;

@property (strong, nonatomic) NSMutableArray *array;

@end

@implementation ProjectTotalViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_type == 1) {
        _navView.hidden = NO;
        [self gettotal];
    } else if (_type == 2) {
        _navViewOne.hidden = NO;
        [self getmoneytotal];
    }
    [self.tableView.header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _array = [NSMutableArray array];
    [self initViewLayout];
    [self addRefreshing];
}
- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.scroview];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.navViewOne];

    _scroview.contentSize = flexibleSize(CGSizeMake(0, 504), NO);
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark -- 添加刷新数据
- (void)addRefreshing{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (_type == 1) {
            if (_buttonOne.selected) {
                [self getdepartment];
            } else if (_buttonTwo.selected) {
                [self getregion];
            } else if (_buttonThree.selected) {
                [self getFixednumberofyear];
            }
        } else if (_type == 2) {
            if (_buttonOne.selected) {
                [self getMoneydepartment];
            } else if (_buttonTwo.selected) {
                [self getMoneyregion];
            } else if (_buttonThree.selected) {
                [self getMoneyFixednumberofyear];
            }
        }
    }];
    [_tableView.header beginRefreshing];
}

#pragma mark - 项目总计
- (void)gettotal {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_projectCount withHttpType:@"POST" withTag:F_F_projectCount];
}
#pragma mark - 资金总计
- (void)getmoneytotal {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_priceProjectCount withHttpType:@"POST" withTag:F_F_priceProjectCount];
}

#pragma mark - 按部门查询项目统计
- (void)getdepartment {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    if (_time) {
        [dic setObject:_time forKey:@"time"];
    }
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_projectCountDepartment withHttpType:@"POST" withTag:F_F_projectCountDepartment];
}
#pragma mark - 按地区查询项目统计
- (void)getregion {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    if (_time) {
        [dic setObject:_time forKey:@"time"];
    }
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_projectCountArea withHttpType:@"POST" withTag:F_F_projectCountArea];
}
#pragma mark - 按年限查询项目统计
- (void)getFixednumberofyear {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    if (_time) {
        [dic setObject:_time forKey:@"time"];
    }
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_projectCountImpleTime withHttpType:@"POST" withTag:F_F_projectCountImpleTime];
}

#pragma mark - 按部门查询资金统计
- (void)getMoneydepartment {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    if (_time) {
        [dic setObject:_time forKey:@"time"];
    }
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_priceProjectDepart withHttpType:@"POST" withTag:F_F_priceProjectDepart];
}
#pragma mark - 按地区查询资金统计
- (void)getMoneyregion {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    if (_time) {
        [dic setObject:_time forKey:@"time"];
    }
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_priceProjectArea withHttpType:@"POST" withTag:F_F_priceProjectArea];
}
#pragma mark - 按年限查询资金统计
- (void)getMoneyFixednumberofyear {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    if (_time) {
        [dic setObject:_time forKey:@"time"];
    }
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_priceProjectImpleTime withHttpType:@"POST" withTag:F_F_priceProjectImpleTime];
}

#pragma mark - 部门 && 地区 && 年限
- (IBAction)button:(UIButton *)sender {
    if (sender == _buttonOne) {
        _buttonOne.selected = YES;
        _buttonTwo.selected = NO;
        _buttonThree.selected = NO;
        _imageButtonOne.selected = YES;
        _imageButtonTwo.selected = NO;
        _imageButtonThree.selected = NO;
        
    } else if (sender == _buttonTwo) {
        _buttonOne.selected = NO;
        _buttonTwo.selected = YES;
        _buttonThree.selected = NO;
        _imageButtonOne.selected = NO;
        _imageButtonTwo.selected = YES;
        _imageButtonThree.selected = NO;
        
    } else if (sender == _buttonThree) {
        _buttonOne.selected = NO;
        _buttonTwo.selected = NO;
        _buttonThree.selected = YES;
        _imageButtonOne.selected = NO;
        _imageButtonTwo.selected = NO;
        _imageButtonThree.selected = YES;
        
    }
    [_tableView.header beginRefreshing];
}

#pragma mark - <UITableViewDelegate&&UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//定义 UITableView cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

//定义 UITableView cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 105*VerticalRatio();
}

//定义 UITableView cell 展示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ProjectTotalTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:tableviewIndentifer];
    if (cell == nil) {
        cell= [[ProjectTotalTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableviewIndentifer];
        
    }
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    NSDictionary *dic = _array[indexPath.row];
    
    if (_type == 1) {
        cell.labelnumber.hidden = YES;
        if (_buttonOne.selected) {
            cell.labelOne.text = [NSString stringWithFormat:@"部门名称：%@",[Common stringFromData:dic[@"DEPARTMENTNAME"]]];
        } else if (_buttonTwo.selected) {
            cell.labelOne.text = [NSString stringWithFormat:@"扶贫地区：%@ %@",[Common stringFromData:dic[@"TOWN"]],[Common stringFromData:dic[@"COUNTRYNAME"]]];
        } else if (_buttonThree.selected) {
            cell.labelOne.text = [NSString stringWithFormat:@"扶贫年限：%@",[Common stringFromData:dic[@"IMPLEMENT_TIME"]]];
        }
        cell.labelTwo.text = [NSString stringWithFormat:@"项目规划个数：%@",[Common stringFromData:dic[@"COUNT"]]];
        cell.labelThree.text = [NSString stringWithFormat:@"未开工项目：%@",[Common stringFromData:dic[@"PRESSTYPE1"]]];
        cell.labelFour.text = [NSString stringWithFormat:@"开工在建项目：%@",[Common stringFromData:dic[@"PRESSTYPE2"]]];
        cell.labelFive.text = [NSString stringWithFormat:@"已完工项目：%@",[Common stringFromData:dic[@"PRESSTYPE3"]]];
        cell.labelSix.text = [NSString stringWithFormat:@"已验收项目：%@",[Common stringFromData:dic[@"PRESSTYPE4"]]];
    } else if (_type == 2) {
        if (_buttonOne.selected) {
            cell.labelOne.text = [NSString stringWithFormat:@"部门名称：%@",[Common stringFromData:dic[@"DEPARTMENTNAME"]]];
        } else if (_buttonTwo.selected) {
            cell.labelOne.text = [NSString stringWithFormat:@"扶贫地区：%@ %@",[Common stringFromData:dic[@"TOWN"]],[Common stringFromData:dic[@"COUNTRYNAME"]]];
        } else if (_buttonThree.selected) {
            cell.labelOne.text = [NSString stringWithFormat:@"扶贫年限：%@",[Common stringFromData:dic[@"IMPLEMENT_TIME"]]];
        }
        cell.labelTwo.text = [NSString stringWithFormat:@"项目规划个数：%@",[Common stringFromData:dic[@"COUNT"]]];
        cell.labelnumber.hidden = NO;
        cell.labelnumber.text = [NSString stringWithFormat:@"项目投资总额：%@万",[Common stringFromData:dic[@"PLANT"]]];
        cell.labelThree.text = [NSString stringWithFormat:@"下达资金项目：%@",[Common stringFromData:dic[@"PROJECT1"]]];
        cell.labelFour.text = [NSString stringWithFormat:@"下达资金总额：%@万",[Common stringFromData:dic[@"PRICE1"]]];
        cell.labelFive.text = [NSString stringWithFormat:@"未下达资金项目：%@",[Common stringFromData:dic[@"PROJECT2"]]];
        cell.labelSix.text = [NSString stringWithFormat:@"未下达资金总额：%@万",[Common stringFromData:dic[@"PRICE2"]]];
    }
    
    return cell;
}

//定义 UITableView cell 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomePageStoryboard" bundle:nil];
    ProjectManagementSonViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ProjectManagementSon"];
    page.type = self.type;
    if (_buttonOne.selected) {
        page.typeTwo = 1;
        page.department = [Common stringFromData:_array[indexPath.row][@"DEPARTMENT"]];
    } else if (_buttonTwo.selected) {
        page.typeTwo = 2;
        page.areaCode = [Common stringFromData:_array[indexPath.row][@"COUNTRY"]];
    } else if (_buttonThree.selected) {
        page.typeTwo = 3;
        page.implementTime = [Common stringFromData:_array[indexPath.row][@"IMPLEMENT_TIME"]];
    }
    [[Common getNavigation]pushViewController:page];
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    [_tableView.header endRefreshing];
    if (tag==F_F_projectCount || tag==F_F_priceProjectCount) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            if (_type == 1) {
            
                _labelOne.text = [NSString stringWithFormat:@"[ 总计规划项目：%@ ]",[Common stringFromData:database[@"data"][@"COUNT"]]];
                _labelTwo.text = [NSString stringWithFormat:@"未开工：%@",[Common stringFromData:database[@"data"][@"PRESSTYPE1"]]];
                _labelThree.text = [NSString stringWithFormat:@"开工在建：%@",[Common stringFromData:database[@"data"][@"PRESSTYPE2"]]];
                _labelFour.text = [NSString stringWithFormat:@"已完工：%@",[Common stringFromData:database[@"data"][@"PRESSTYPE3"]]];
                _labelFive.text = [NSString stringWithFormat:@"已验收：%@",[Common stringFromData:database[@"data"][@"PRESSTYPE4"]]];
            } else if (_type == 2) {
                _oneLabel.text = [NSString stringWithFormat:@"[ 总计规划项目：%@ ]",[Common stringFromData:database[@"data"][@"COUNT"]]];
                _twoLabel.text = [NSString stringWithFormat:@"[ 规划投资总额：%@万 ]",[Common stringFromData:database[@"data"][@"PLANT"]]];
                _sanLabel.text = [NSString stringWithFormat:@"下达资金项目：%@",[Common stringFromData:database[@"data"][@"PROJECT1"]]];
                _threeLabel.text = [NSString stringWithFormat:@"下达资金总额：%@万",[Common stringFromData:database[@"data"][@"PRICE1"]]];
                _fourLabel.text = [NSString stringWithFormat:@"未下达资金项目：%@",[Common stringFromData:database[@"data"][@"PROJECT2"]]];
                _fiveLabel.text =  [NSString stringWithFormat:@"未下达资金总额：%@万",[Common stringFromData:database[@"data"][@"PRICE2"]]];
            }
            
        }else  {
        }
    } else if (tag==F_F_projectCountDepartment || tag==F_F_projectCountArea || tag==F_F_projectCountImpleTime || tag==F_F_priceProjectDepart || tag==F_F_priceProjectArea || tag==F_F_priceProjectImpleTime) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            _array = database[@"data"];
            [self.tableView reloadData];
        }else  {
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    [_tableView.header endRefreshing];
    [self dismissWaitDialog];
    
}

@end
